package com.game.modular.game.battle.panel.attribute;

import cn.hutool.core.bean.BeanUtil;
import com.game.comment.util.RedisUtils;
import com.game.modular.game.battle.panel.attribute.domain.PlayerHeroAttribute;
import com.game.modular.game.playerAssets.domain.PlayerHero;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * 基础服务-武将属性
 *
 * @author cxy
 * @since 2024-09-24 14:45:31
 */
@Slf4j
@Service
public class PlayerHeroAttributeBaseServiceImpl {
    public static final String PlayerHeroAttribute_key = "battle:%d:panel:battleInfo:attribute";
    public static final String PlayerHeroAttribute_base_key = "battle:%d:panel:battleInfo:attributeBase";
    @Resource
    public RedisUtils redisUtils;
    @Resource
    private RedisTemplate<Object, Object> redisTemplate;
    @Resource
    private HashOperations<Object, String, PlayerHeroAttribute> hashOperations;

    public void init(@NotNull(message = "战斗id不能为空") Long battleId, List<PlayerHero> actionList) {
        HashSet<PlayerHero> hashSet = new HashSet<>(actionList);
        for (PlayerHero playerHero : hashSet) {
            PlayerHeroAttribute bean = BeanUtil.toBean(playerHero, PlayerHeroAttribute.class);
            bean.setPhase(0);// 战斗中
            bean.setBattleId(battleId);
            bean.event=new HashMap<>();
            bean.setBehaviorMap(new HashMap<>());
            removeCache(battleId, bean.getId());
            redisUtils.hPut(
                    String.format(PlayerHeroAttribute_key, battleId),
                    bean.getId().toString(),
                    bean,
                    20,
                    TimeUnit.MINUTES
            );
            redisUtils.hPut(
                    String.format(PlayerHeroAttribute_base_key, battleId),
                    bean.getId().toString(),
                    bean,
                    20,
                    TimeUnit.MINUTES
            );
        }
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////  基础查询
    public List<PlayerHeroAttribute> eqList(PlayerHeroAttribute select) {
        if (select.getBattleId() == null) {
            throw new RuntimeException("keyValue1不能为空");
        }
        Long keyValue1 = select.getBattleId();
        return listCache(keyValue1).stream().filter(item ->
                 select.getId()==null || Objects.equals(item.getId(), select.getId())
              && select.getAtt()==null || Objects.equals(item.getAtt(), select.getAtt())
              && select.getDef()==null || Objects.equals(item.getDef(), select.getDef())
              && select.getMagic()==null || Objects.equals(item.getMagic(), select.getMagic())
              && select.getSpeed()==null || Objects.equals(item.getSpeed(), select.getSpeed())
              && select.getDes()==null || Objects.equals(item.getDes(), select.getDes())
              && select.getCurrentForce()==null || Objects.equals(item.getCurrentForce(), select.getCurrentForce())
        ).collect(Collectors.toList());
    }
    
    public PlayerHeroAttribute eqSelect(PlayerHeroAttribute select) {
        return eqList(select).stream().findFirst().orElse(null);
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////    转换
    public Map<String, PlayerHeroAttribute> listToMap(List<PlayerHeroAttribute> list) {
        return list.stream().collect(Collectors.toMap(
                k -> k.getId().toString(),
                v -> v
        ));
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////  缓存部分
    public void saveCache(PlayerHeroAttribute entity) {
        if (entity.getCurrentForce() == null){
            throw new RuntimeException("当前兵力不能为空");
        }
        String key1 = String.format(PlayerHeroAttribute_key, entity.getBattleId());
        String key2 = entity.getId().toString();
        hashOperations.put(key1, key2, entity);
        redisTemplate.expire(key1, 60, TimeUnit.MINUTES);
    }
    public PlayerHeroAttribute selectCache(
            @NotNull(message = "战斗id不能为空") Long battleId,
            @NotNull(message = "玩家英雄id不能为空") Long playerHeroId
    ) {
        String key1 = String.format(PlayerHeroAttribute_key, battleId);
        String key2 = playerHeroId.toString();
        redisTemplate.expire(key1, 60, TimeUnit.MINUTES);
        return hashOperations.get(key1, key2);
    }
    public List<PlayerHeroAttribute> listCache(
            @NotNull(message = "keyValue1不能为空") Long battleId

    ) {
        String key1 = String.format(PlayerHeroAttribute_key, battleId);
        redisTemplate.expire(key1, 60, TimeUnit.MINUTES);
        return new ArrayList<>(hashOperations.entries(key1).values());
    }

    public Map<String, PlayerHeroAttribute> mapCache(
            @NotNull(message = "keyValue1不能为空") Long keyValue1
    ) {
        String key1 = String.format(PlayerHeroAttribute_key, keyValue1);
        redisTemplate.expire(key1, 60, TimeUnit.MINUTES);
        return hashOperations.entries(key1);
    }
    public void removeCache(
            @NotNull(message = "战斗id不能为空") Long battleId,
            @NotNull(message = "玩家英雄id不能为空") Long id
    ) {
        String key1 = String.format(PlayerHeroAttribute_key, battleId);
        String key2 = id.toString();
        hashOperations.delete(key1, key2);
    } 
}
